class ItemOptionsController < ApplicationController
  before_filter :find_item_option,  :only => [:update,  :destroy]
  before_filter :require_admin

  def index
    @item_options = ItemOption.find(:all)
    @item_option  = ItemOption.new
  end

  def show
  end

  def new
    @item_option = ItemOption.new
  end

  def create
    @item_option = ItemOption.new(params[:item_option])
    @item_option.for_photo = false if params[:item_option][:for_video] == "1"
    @item_option.for_photo = true if params[:item_option][:for_video] == "0"
    @item_option.save!
    flash[:update] = 'The Item option has been added.'
    redirect_to settings_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    redirect_to settings_url
  end

  def edit
  end

  def update
    @item_option.for_photo = false if params[:item_option][:for_video] == "1"
    @item_option.for_photo = true if params[:item_option][:for_video] == "0"
    @item_option.update_attributes!(params[:item_option])
    flash[:update] = 'Item option has been changed.'      
    redirect_to settings_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    redirect_to settings_url
  end

  def destroy
    @item_option.destroy
    flash[:update] = 'The Item option has been deleted.'
    redirect_to settings_url
  end

protected
  def require_admin
    if logged_in?
      if !current_user.is_admin?
        flash[:error] = "Must be an administrator to do that, sorry."
        redirect_back_or_default('/')
      end
    else
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def find_item_option
    @item_option = ItemOption.find params[:id]
  end
end
